package com.feeyin.radar.crm.service.impl;

import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.feeyin.radar.commons.DateUtils;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.dao.ContractBackCostDAO;
import com.feeyin.radar.crm.dao.ContractDAO;
import com.feeyin.radar.crm.dao.CustomerDAO;
import com.feeyin.radar.crm.form.BackCostForm;
import com.feeyin.radar.crm.form.BackCostNewForm;
import com.feeyin.radar.crm.form.ExportBackCostForm;
import com.feeyin.radar.crm.model.BackCost;
import com.feeyin.radar.crm.model.BackCostHistory;
import com.feeyin.radar.crm.model.BackCostNew;
import com.feeyin.radar.crm.model.Contract;
import com.feeyin.radar.crm.model.Customer;
import com.feeyin.radar.crm.service.ContractBackCostService;
import com.feeyin.radar.sys.dao.DataDictionaryDAO;
import com.feeyin.radar.sys.dao.DepartmentDAO;
import com.feeyin.radar.sys.dao.RecordRangeDAO;
import com.feeyin.radar.sys.dao.UserDAO;
import com.feeyin.radar.sys.model.DataDictionary;
import com.feeyin.radar.sys.model.Department;
import com.feeyin.radar.sys.model.Users;

public class ContractBackCostServiceImpl
  implements ContractBackCostService
{
  private ContractBackCostDAO contractBackCostDAO;
  private ContractDAO contractDAO;
  private CustomerDAO customerDAO;
  private UserDAO userDAO;
  private DepartmentDAO departmentDAO;
  private DataDictionaryDAO dataDictionaryDAO;
  private RecordRangeDAO recordRangeDAO;

  public ContractBackCostDAO getContractBackCostDAO()
  {
    return this.contractBackCostDAO;
  }

  public void setContractBackCostDAO(ContractBackCostDAO contractBackCostDAO) {
    this.contractBackCostDAO = contractBackCostDAO;
  }

  public ContractDAO getContractDAO() {
    return this.contractDAO;
  }

  public void setContractDAO(ContractDAO contractDAO) {
    this.contractDAO = contractDAO;
  }

  public CustomerDAO getCustomerDAO() {
    return this.customerDAO;
  }

  public void setCustomerDAO(CustomerDAO customerDAO) {
    this.customerDAO = customerDAO;
  }

  public UserDAO getUserDAO() {
    return this.userDAO;
  }

  public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
  }

  public DepartmentDAO getDepartmentDAO() {
    return this.departmentDAO;
  }

  public void setDepartmentDAO(DepartmentDAO departmentDAO) {
    this.departmentDAO = departmentDAO;
  }

  public DataDictionaryDAO getDataDictionaryDAO() {
    return this.dataDictionaryDAO;
  }

  public void setDataDictionaryDAO(DataDictionaryDAO dataDictionaryDAO) {
    this.dataDictionaryDAO = dataDictionaryDAO;
  }

  public RecordRangeDAO getRecordRangeDAO() {
    return this.recordRangeDAO;
  }

  public void setRecordRangeDAO(RecordRangeDAO recordRangeDAO) {
    this.recordRangeDAO = recordRangeDAO;
  }

  public Map getBackCostDictionaryRanges() {
    Map map = new HashMap();

    List customerDictionarys = new ArrayList();
    customerDictionarys = this.dataDictionaryDAO.getDataDictionarysByRecordType("3");
    if (customerDictionarys != null) {
      for (int i = 0; i < customerDictionarys.size(); i++) {
        DataDictionary dataDictionary = new DataDictionary();
        dataDictionary = (DataDictionary)customerDictionarys.get(i);
        List ranges = this.recordRangeDAO.getActivityRecordRangesByDictionaryId(dataDictionary.getId());
        if (("32".equals(dataDictionary.getRecordCode())) || ("33".equals(dataDictionary.getRecordCode())) 
        		|| ("35".equals(dataDictionary.getRecordCode())|| ("36".equals(dataDictionary.getRecordCode())))) {
          map.put(dataDictionary.getRecordCode(), ranges);
        }
      }
    }
    return map;
  }

  public List getUsers(String isAllBackCost, String sign, Integer currentUserId)
  {
    List users = new ArrayList();
    if ((!StringUtils.isEmpty(isAllBackCost)) && ("1".equals(isAllBackCost))) {
      Users user = new Users();
      user.setIsAdmin(Integer.valueOf(1));
      users = this.userDAO.getUsersByDynamic(user);
    } else if ((!StringUtils.isEmpty(sign)) && ("2".equals(sign)) && (currentUserId != null)) {
      Users user = new Users();
      user = this.userDAO.getUser(currentUserId);
      if (user == null) return null;
      String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
      if (StringUtils.isEmpty(deptIds)) return null;
      user = new Users();
      user.setIsAdmin(Integer.valueOf(1));
      user.setDeptIds(deptIds);
      users = this.userDAO.getUsersByDynamic(user);
    }
    if (users != null) {
      for (int i = 0; i < users.size(); i++) {
        Users user = new Users();
        user = (Users)users.get(i);
        Department dept = new Department();
        dept = this.departmentDAO.getDepartment(user.getDeptId());
        if (dept != null) {
          user.setDeptName(dept.getDeptName());
        }
      }
    }
    return users;
  }

  public List getBackCosts(Integer contractId) {
    if (contractId == null) return null;
    List backCosts = new ArrayList();
    backCosts = this.contractBackCostDAO.getBackCosts(contractId);
    if (backCosts != null) {
      for (int i = 0; i < backCosts.size(); i++) {
        BackCost backCost = new BackCost();
        backCost = (BackCost)backCosts.get(i);
        Users user = new Users();
        user = this.userDAO.getUser(backCost.getSalePersonId());
        if (user != null) {
          backCost.setSalePerson(user.getUserName());
        }
      }
    }
    return backCosts;
  }

  public List getPageMyContracts(String isAllBackCost, String customerName, Integer currentUserId, Page page)
  {
    if (currentUserId == null) return null;
    if (page == null) return null;
    Contract contract = new Contract();

    if ((!StringUtils.isEmpty(isAllBackCost)) && 
      ("1".equals(isAllBackCost))) {
      contract.setIsAllBackCost("1");
    } else if ((!StringUtils.isEmpty(isAllBackCost)) && 
      ("2".equals(isAllBackCost))) {
      Users user = new Users();
      user = this.userDAO.getUser(currentUserId);
      if ((user == null) || (user.getDeptId() == null)) return null;
      String deptIds = "";
      deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
      if (StringUtils.isEmpty(deptIds)) return null;
      contract.setDeptIds(deptIds);
    } else {
      contract.setSalePersonId(currentUserId);
    }
    if (!StringUtils.isEmpty(customerName)) {
      contract.setCustomerName(customerName);
    }
    contract.setStatus("2");
    List contracts = new ArrayList();
    contracts = this.contractDAO.getPageContractsByDynamic(contract, page);
    if (contracts == null) return null;
    for (int i = 0; i < contracts.size(); i++) {
      contract = new Contract();
      contract = (Contract)contracts.get(i);

      Customer customer = new Customer();
      customer = this.customerDAO.getCustomerById(contract.getCustomerId());
      if (customer != null) {
        contract.setCustomerName(customer.getCustomerName());
      }
      Users user = new Users();
      user = this.userDAO.getUser(contract.getSalePersonId());
      if (user != null) {
        contract.setSalePerson(user.getUserName());
      }
    }
    return contracts;
  }

  public Integer getPageMyContractsCount(String isAllBackCost, String customerName, Integer currentUserId)
  {
    if (currentUserId == null) return null;
    Contract contract = new Contract();

    if ((!StringUtils.isEmpty(isAllBackCost)) && 
      ("1".equals(isAllBackCost))) {
      contract.setIsAllBackCost("1");
    } else if ((!StringUtils.isEmpty(isAllBackCost)) && 
      ("2".equals(isAllBackCost))) {
      Users user = new Users();
      user = this.userDAO.getUser(currentUserId);
      if ((user == null) || (user.getDeptId() == null)) return null;
      String deptIds = "";
      deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
      if (StringUtils.isEmpty(deptIds)) return null;
      contract.setDeptIds(deptIds);
    } else {
      contract.setSalePersonId(currentUserId);
    }
    if (!StringUtils.isEmpty(customerName)) {
      contract.setCustomerName(customerName);
    }
    contract.setStatus("2");
    return this.contractDAO.getPageContractsCountByDynamic(contract);
  }

  public boolean exportBackCosts(String realPath, String isAllBackCost, String sign, Integer currentUserId, String[] userIds, ExportBackCostForm exportBackCostForm)
  {
    if (StringUtils.isEmpty(realPath)) return false;
    if (exportBackCostForm == null) return false;
    if (currentUserId == null) return false;
    List contracts = new ArrayList();
    Contract contract = new Contract();
    if ((exportBackCostForm.getStartDay() != null) && (exportBackCostForm.getEndDay() != null)) {
      contract.setStartDay(exportBackCostForm.getStartDay());
      contract.setEndDay(exportBackCostForm.getEndDay());
    }
    if ((exportBackCostForm.getStartDate() != null) && (exportBackCostForm.getEndDate() != null)) {
      contract.setStartDate(exportBackCostForm.getStartDate());
      contract.setEndDate(exportBackCostForm.getEndDate());
    }
    if (((!StringUtils.isEmpty(isAllBackCost)) && ("1".equals(isAllBackCost))) || (
      (!StringUtils.isEmpty(sign)) && ("2".equals(sign)))) {
      if ((userIds == null) || (userIds.length < 0)) return false;
      String salePersonIds = "";
      for (int i = 0; i < userIds.length; i++) {
        if (!StringUtils.isEmpty(userIds[i])) {
          if (i < userIds.length - 1)
            salePersonIds = salePersonIds + userIds[i] + ",";
          else {
            salePersonIds = salePersonIds + userIds[i];
          }
        }
      }
      if (StringUtils.isEmpty(salePersonIds)) return false;
      contract.setSalePersonIds(salePersonIds);
    } else {
      contract.setSalePersonId(currentUserId);
    }
    contracts = this.contractDAO.getContractsByDynamic(contract);
    if ((contracts == null) || (contracts.size() < 0)) return false;

    return saveWaitExportFile(realPath, currentUserId, contracts);
  }

  public boolean insertBackCost(BackCostForm backCostForm) {
    if (backCostForm == null) return false;
    if (backCostForm.getContractId() == null) return false;
    if ((backCostForm.getCost() == null) || (backCostForm.getCost().length <= 0)) return false;
    if ((backCostForm.getIncomeTypeId() == null) || (backCostForm.getIncomeTypeId().length <= 0)) return false;
    if ((backCostForm.getBackCostTypeId() == null) || (backCostForm.getBackCostTypeId().length <= 0)) return false;
    if ((backCostForm.getBackCostWayId() == null) || (backCostForm.getBackCostWayId().length <= 0)) return false;
    if ((backCostForm.getBackDate() == null) || (backCostForm.getBackDate().length <= 0)) return false;
    if ((backCostForm.getSalePersonId() == null) || (backCostForm.getSalePersonId().length <= 0)) return false;

    for (int i = 0; i < backCostForm.getCost().length; i++) {
      if ((backCostForm.getCost()[i] == null) || (backCostForm.getCost()[i].compareTo(new BigDecimal(0)) <= 0) || 
        (backCostForm.getBackCostTypeId()[i] == null) || (backCostForm.getBackCostTypeId()[i].intValue() <= 0) || 
        (backCostForm.getBackCostWayId()[i] == null) || (backCostForm.getBackCostWayId()[i].intValue() <= 0) || 
        (backCostForm.getBackDate()[i] == null) || 
        (backCostForm.getSalePersonId()[i] == null) || 
        (backCostForm.getIncomeTypeId()[i] == null)) continue;
      BackCost backCost = new BackCost();
      backCost.setContractId(backCostForm.getContractId());
      backCost.setBackCostCode(new Long(new Date().getTime()).toString());
      backCost.setCost(backCostForm.getCost()[i]);
      backCost.setIncomeTypeId(backCostForm.getIncomeTypeId()[i]);
      backCost.setBackCostTypeId(backCostForm.getBackCostTypeId()[i]);
      backCost.setBackCostWayId(backCostForm.getBackCostWayId()[i]);
      backCost.setBackDate(backCostForm.getBackDate()[i]);
      backCost.setSalePersonId(backCostForm.getSalePersonId()[i]);
      backCost.setCreateDate(new Date());
      if ((backCostForm.getBackCostDesc() != null) && (backCostForm.getBackCostDesc().length > 0)) {
        backCost.setBackCostDesc(backCostForm.getBackCostDesc()[i]);
      }
      if ((backCostForm.getGrossBackCostId() != null) && (backCostForm.getGrossBackCostId().length > 0)) {
        backCost.setGrossBackCostId(backCostForm.getGrossBackCostId()[i]);
      }
      if ((backCostForm.getBusinessStatus() != null) && (backCostForm.getBusinessStatus().length > 0)) {
        backCost.setBusinessStatus(backCostForm.getBusinessStatus()[i]);
      }
      this.contractBackCostDAO.insertBackCost(backCost);
    }

    Contract contract = new Contract();
    contract = this.contractDAO.getContractById(backCostForm.getContractId());

    List backCosts = new ArrayList();
    backCosts = this.contractBackCostDAO.getBackCosts(backCostForm.getContractId());

    if ((contract != null) && (backCosts != null) && (backCosts.size() > 0)) {
      BigDecimal totalBackCost = this.contractBackCostDAO.getTotalBackCost(backCostForm.getContractId());
      BigDecimal receivable = new BigDecimal(0);
      Integer backCostNum = Integer.valueOf(0);
      backCostNum = Integer.valueOf(backCosts.size());

      if ((contract.getContractCost() != null) && (contract.getContractCost().compareTo(new BigDecimal(0)) > 0)) {
        receivable = contract.getContractCost().subtract(totalBackCost);
      }

      contract = new Contract();
      contract.setId(backCostForm.getContractId());
      contract.setBackCost(totalBackCost);
      contract.setReceivable(receivable);
      contract.setBackCostNum(backCostNum);

      this.contractDAO.updateContractByDynamic(contract);
    }
    return true;
  }

  public boolean updateBackCost(BackCostForm backCostForm) {
    if (backCostForm == null) return false;
    if ((backCostForm.getId() == null) || (backCostForm.getId().length <= 0)) return false;
    if (backCostForm.getContractId() == null) return false;
    if (backCostForm.getIncomeTypeId() == null) return false;
    if ((backCostForm.getCost() == null) || (backCostForm.getCost().length <= 0)) return false;
    if ((backCostForm.getBackCostTypeId() == null) || (backCostForm.getBackCostTypeId().length <= 0)) return false;
    if ((backCostForm.getBackCostWayId() == null) || (backCostForm.getBackCostWayId().length <= 0)) return false;
    if ((backCostForm.getBackDate() == null) || (backCostForm.getBackDate().length <= 0)) return false;
    if ((backCostForm.getSalePersonId() == null) || (backCostForm.getSalePersonId().length <= 0)) return false;

    for (int i = 0; i < backCostForm.getCost().length; i++) {
      if ((backCostForm.getId()[i] != null) && 
        (backCostForm.getCost()[i] != null) && (backCostForm.getCost()[i].compareTo(new BigDecimal(0)) > 0) && 
        (backCostForm.getBackCostTypeId()[i] != null) && (backCostForm.getBackCostTypeId()[i].intValue() > 0) && 
        (backCostForm.getBackCostWayId()[i] != null) && (backCostForm.getBackCostWayId()[i].intValue() > 0) && 
        (backCostForm.getBackDate()[i] != null) && 
        (backCostForm.getSalePersonId()[i] != null) && 
        (backCostForm.getIncomeTypeId()[i] != null))
      {
        BackCost backCost = new BackCost();
        backCost.setId(backCostForm.getId()[i]);

        backCost.setCost(backCostForm.getCost()[i]);
        backCost.setIncomeTypeId(backCostForm.getIncomeTypeId()[i]);
        backCost.setBackCostTypeId(backCostForm.getBackCostTypeId()[i]);
        backCost.setBackCostWayId(backCostForm.getBackCostWayId()[i]);
        backCost.setBackDate(backCostForm.getBackDate()[i]);
        backCost.setSalePersonId(backCostForm.getSalePersonId()[i]);

        if ((backCostForm.getBackCostDesc() != null) && (backCostForm.getBackCostDesc().length > 0)) {
          backCost.setBackCostDesc(backCostForm.getBackCostDesc()[i]);
        }
        if ((backCostForm.getGrossBackCostId() != null) && (backCostForm.getGrossBackCostId().length > 0)) {
          backCost.setGrossBackCostId(backCostForm.getGrossBackCostId()[i]);
        }
        if ((backCostForm.getBusinessStatus() != null) && (backCostForm.getBusinessStatus().length > 0)) {
          backCost.setBusinessStatus(backCostForm.getBusinessStatus()[i]);
        }
        this.contractBackCostDAO.updateBackCost(backCost);
      } else if ((backCostForm.getId()[i] == null) && (backCostForm.getCost()[i] != null) && (backCostForm.getCost()[i].compareTo(new BigDecimal(0)) > 0) && 
        (backCostForm.getBackCostTypeId()[i] != null) && (backCostForm.getBackCostTypeId()[i].intValue() > 0) && 
        (backCostForm.getBackCostWayId()[i] != null) && (backCostForm.getBackCostWayId()[i].intValue() > 0) && 
        (backCostForm.getBackDate()[i] != null) && 
        (backCostForm.getSalePersonId()[i] != null) && 
        (backCostForm.getIncomeTypeId()[i] != null)) {
        BackCost backCost = new BackCost();
        backCost.setContractId(backCostForm.getContractId());
        backCost.setBackCostCode(new Long(new Date().getTime()).toString());
        backCost.setCost(backCostForm.getCost()[i]);
        backCost.setIncomeTypeId(backCostForm.getIncomeTypeId()[i]);
        backCost.setBackCostTypeId(backCostForm.getBackCostTypeId()[i]);
        backCost.setBackCostWayId(backCostForm.getBackCostWayId()[i]);
        backCost.setBackDate(backCostForm.getBackDate()[i]);
        backCost.setSalePersonId(backCostForm.getSalePersonId()[i]);
        backCost.setCreateDate(new Date());

        if ((backCostForm.getGrossBackCostId() != null) && (backCostForm.getGrossBackCostId().length > 0)) {
          backCost.setGrossBackCostId(backCostForm.getGrossBackCostId()[i]);
        }
        if ((backCostForm.getBusinessStatus() != null) && (backCostForm.getBusinessStatus().length > 0)) {
          backCost.setBusinessStatus(backCostForm.getBusinessStatus()[i]);
        }
        this.contractBackCostDAO.insertBackCost(backCost);
      } else if ((backCostForm.getId()[i] != null) && (backCostForm.getCost()[i].compareTo(new BigDecimal(0)) == 0)) {
        this.contractBackCostDAO.deleteBackCost(backCostForm.getId()[i]);
      }
    }

    Contract contract = new Contract();
    contract = this.contractDAO.getContractById(backCostForm.getContractId());

    if (contract != null) {
      BigDecimal totalBackCost = this.contractBackCostDAO.getTotalBackCost(backCostForm.getContractId());
      BigDecimal receivable = new BigDecimal(0);

      if ((contract.getContractCost() != null) && (contract.getContractCost().compareTo(new BigDecimal(0)) > 0) && (totalBackCost != null))
        receivable = contract.getContractCost().subtract(totalBackCost);
      else {
        receivable = contract.getContractCost().subtract(new BigDecimal(0));
      }

      contract = new Contract();
      contract.setId(backCostForm.getContractId());
      if (totalBackCost == null)
        contract.setBackCost(new BigDecimal(0));
      else {
        contract.setBackCost(totalBackCost);
      }
      contract.setReceivable(receivable);
      this.contractDAO.updateContractByDynamic(contract);
    }
    return true;
  }

  public boolean saveWaitExportFile(String realPath, Integer currentUserId, List contracts)
  {
    if (StringUtils.isEmpty(realPath)) return false;
    if (currentUserId == null) return false;
    if ((contracts == null) || (contracts.size() < 0)) return false;
    String basePath = realPath + "upload" + System.getProperty("file.separator") + "temp";
    String storedPath = basePath + System.getProperty("file.separator") + "contractBackCost" + currentUserId + ".xls";
    System.out.println("storedPath = " + storedPath);
    try
    {
      FileOutputStream fos = new FileOutputStream(storedPath);
      HSSFWorkbook wb = new HSSFWorkbook();
      HSSFSheet s = wb.createSheet();
      wb.setSheetName(0, "sheet1");
      HSSFRow row = s.createRow(0);

      HSSFFont fontStyle = wb.createFont();

      fontStyle.setFontHeightInPoints((short)12);
      fontStyle.setBoldweight((short)700);

      HSSFCellStyle cellStyle = wb.createCellStyle();
      cellStyle.setFillForegroundColor((short)12);
      cellStyle.setFont(fontStyle);
      cellStyle.setFillPattern((short)1);

      s.setDefaultColumnWidth((short)18);

      HSSFCell cell = row.getCell((short)0);
      cell.setCellStyle(cellStyle);
      cell.setCellValue("合同编码");
      cell.setCellStyle(cellStyle);
      cell = row.getCell((short)1);
      cell.setCellValue("合同名称");
      cell.setCellStyle(cellStyle);
      cell = row.getCell((short)2);
      cell.setCellValue("合同金额(元)");
      cell.setCellStyle(cellStyle);
      cell = row.getCell((short)3);
      cell.setCellValue("应收帐款(元)");
      cell.setCellStyle(cellStyle);
      cell = row.getCell((short)4);
      cell.setCellValue("已回款(元)");
      cell.setCellStyle(cellStyle);
      cell = row.getCell((short)5);
      cell.setCellStyle(cellStyle);
      cell.setCellValue("建账日期");
      cell = row.getCell((short)6);
      cell.setCellStyle(cellStyle);
      cell.setCellValue("账龄(天)");
      cell = row.getCell((short)7);
      cell.setCellStyle(cellStyle);
      cell.setCellValue("销售代表");
      for (int i = 0; i < contracts.size(); i++) {
        Contract contract = new Contract();
        contract = (Contract)contracts.get(i);
        row = s.createRow(i + 1);
        cell = row.getCell((short)0);
        cell.setCellValue(contract.getContractCode());
        cell = row.getCell((short)1);
        cell.setCellValue(contract.getContractName());
        cell = row.getCell((short)2);
        cell.setCellValue(contract.getContractCost().toString());
        cell = row.getCell((short)3);
        cell.setCellValue(contract.getReceivable().toString());
        cell = row.getCell((short)4);
        cell.setCellValue(contract.getBackCost().toString());
        cell = row.getCell((short)5);
        cell.setCellValue(DateUtils.getStringDateByFormat(contract.getContractDate(), "yyyy-MM-dd"));
        cell = row.getCell((short)6);
        cell.setCellValue(contract.getAging().intValue());
        cell = row.getCell((short)7);
        Users user = new Users();
        user = this.userDAO.getUser(contract.getSalePersonId());
        String salePerson = "";
        if (user != null) {
          salePerson = user.getUserName();
        }
        cell.setCellValue(salePerson);
      }

      wb.write(fos);
      fos.close();
      return true;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }
  
  public boolean insertBackCostNew(BackCostNewForm backCostNewForm)
  {
	  BackCostNew backCostNew = new BackCostNew();
	  backCostNew.setBackCostNum(backCostNewForm.getCostNum());
	  backCostNew.setBackCostTypeId(backCostNewForm.getCostTypeId());
	  backCostNew.setBackCostDesc(backCostNewForm.getBackCostDesc());
	  backCostNew.setBackDate(backCostNewForm.getBackDate());
	  backCostNew.setContractId(backCostNewForm.getContractId());
	  
	  List<BackCostHistory> costHistoryList = new ArrayList<BackCostHistory>();
	  for(int i=0; i<backCostNewForm.getContractCaseId().length; i++)
	  {
		  BackCostHistory history = new BackCostHistory();
		  history.setContractCaseId(backCostNewForm.getContractCaseId()[i]);
		  history.setOfficialCost(backCostNewForm.getOfficialCost()[i]);
		  history.setOperationCost(backCostNewForm.getOperationCost()[i]);
		  history.setProxyCost(backCostNewForm.getProxyCost()[i]);
		  costHistoryList.add(history);
	  }
	  
	  backCostNew.setBackCostHistoryList(costHistoryList);
	  contractBackCostDAO.insertBackCostNew(backCostNew);
	  
	  return true;
  }
  
  public boolean updateBackCostNew(BackCostNewForm backCostNewForm)
  {
	  BackCostNew backCostNew = new BackCostNew();
	  backCostNew.setId(backCostNewForm.getId());
	  backCostNew.setBackCostNum(backCostNewForm.getCostNum());
	  backCostNew.setBackCostTypeId(backCostNewForm.getCostTypeId());
	  backCostNew.setBackCostDesc(backCostNewForm.getBackCostDesc());
	  backCostNew.setBackDate(backCostNewForm.getBackDate());
	  backCostNew.setContractId(backCostNewForm.getContractId());
	  
	  List<BackCostHistory> costHistoryList = new ArrayList<BackCostHistory>();
	  for(int i=0; i<backCostNewForm.getContractCaseId().length; i++)
	  {
		  BackCostHistory history = new BackCostHistory();
		  history.setBackCostId(backCostNew.getId());
		  history.setContractCaseId(backCostNewForm.getContractCaseId()[i]);
		  history.setOfficialCost(backCostNewForm.getOfficialCost()[i]);
		  history.setOperationCost(backCostNewForm.getOperationCost()[i]);
		  history.setProxyCost(backCostNewForm.getProxyCost()[i]);
		  costHistoryList.add(history);
	  }
	  
	  backCostNew.setBackCostHistoryList(costHistoryList);
	  contractBackCostDAO.updateBackCostNew(backCostNew);
	  
	  return true;
  }  
  
  public boolean isDuplicateBackCostNew(String backCostNum)
  {
	  return contractBackCostDAO.isDuplicateBackCostNew(backCostNum);
  } 
  
  public List<BackCostNew> getBackCostNewListByContractId(String contractId)
  {
	  return contractBackCostDAO.getBackCostNewListByContractId(contractId);
  }
  
  public BackCostNew getBackCostNewById(String backCostId)
  {
	  return contractBackCostDAO.getBackCostNewById(backCostId);
  }
}